package com.ywh.composetest.activity

import android.os.Bundle
import android.util.Log
import androidx.compose.foundation.ExperimentalFoundationApi
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxHeight
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.pager.HorizontalPager
import androidx.compose.foundation.pager.rememberPagerState
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.MutableState
import androidx.compose.runtime.mutableIntStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.unit.dp
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch

/**
 * Pager
 * 1.实现主页，一个Activity+多个Fragment
 * 2.实现TabLayout+ViewPager
 * 3.实现banner
 */
class PagerActivity : BaseComposeActivity() {

    private val tabList by lazy { arrayOf("首页", "动态", "我的") }
    @Composable
    override fun OnComposable(savedInstanceState: Bundle?) {
        ContentPager()
    }

    @OptIn(ExperimentalFoundationApi::class)
    @Composable
    private fun ContentPager() {
        val scope = rememberCoroutineScope()

        val pagerState = rememberPagerState(initialPage = 1, initialPageOffsetFraction = -0.5f) {
            tabList.size
        }
        val countState = remember {
            mutableIntStateOf(1)
        }
        Column {
            HorizontalPager(
                modifier = Modifier.weight(1f),
                state = pagerState,
                userScrollEnabled = true,
                beyondBoundsPageCount = tabList.size, //预加载
                key = {
                    it
                },

            ) {
                ContentItem(it, countState)
            }
            Row(
                modifier = Modifier
                    .fillMaxWidth()
                    .height(44.dp)
            )
            {
                tabList.forEachIndexed { index, s ->
                    Box(
                        modifier = Modifier
                            .weight(1f)
                            .fillMaxHeight()
                            .clickable {
                                scope.launch(Dispatchers.Main) {
                                    pagerState.scrollToPage(index)
                                }
                            }, contentAlignment = Alignment.Center
                    ) {
                        Text(
                            text = s,
                            color = if (pagerState.currentPage == index) Color.Red else Color.Black
                        )
                    }
                }
            }
        }
    }

    @Composable
    private fun ContentItem(index: Int, countState: MutableState<Int>) {
        Log.e("PagerActivity", "index:${index},countState:${countState.value}")

        Box(
            modifier = Modifier
                .fillMaxSize()
                .background(
                    when (index) {
                        0 -> Color.Blue
                        1 -> Color.Red
                        2 -> Color.Yellow
                        else -> Color.Blue
                    }
                )
                .clickable {
                    countState.value++
                },
            contentAlignment = Alignment.Center
        ) {
            Text(text = index.toString() + "${countState.value}")
        }
    }

}